package org.jeecg.modules.friends.dao.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.friends.condition.VipUserCondition;
import org.jeecg.modules.friends.mapper.ext.VipUserDOExtMapper;
import org.jeecg.modules.friends.model.VipUserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VipUserDAOImpl implements org.jeecg.modules.friends.dao.VipUserDAO {
    @Autowired
    private VipUserDOExtMapper vipUserDOExtMapper;

    @Override
    public VipUserDO findByUserId(Long userId) {
        VipUserCondition vipUserCondition = new VipUserCondition();
        VipUserCondition.Criteria criteria = vipUserCondition.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<VipUserDO> vipList = vipUserDOExtMapper.selectByCondition(vipUserCondition);
        return CollectionUtils.isEmpty(vipList) ? null : vipList.get(0);
    }

    @Override
    public void minusTicket(Long id) {
        vipUserDOExtMapper.minusTicket(id);
    }

    @Override
    public void addTicket(Long id, int count) {
        vipUserDOExtMapper.addTicket(id, count);
    }

    @Override
    public void insert(VipUserDO vipUserDO) {
        vipUserDOExtMapper.insert(vipUserDO);
    }

    @Override
    public void update(VipUserDO vipUserDO) {
        vipUserDOExtMapper.updateByPrimaryKeySelective(vipUserDO);
    }

    @Override
    public void deleteByUserId(Long userId) {
        VipUserCondition vipUserCondition = new VipUserCondition();
        VipUserCondition.Criteria criteria = vipUserCondition.createCriteria();
        criteria.andUserIdEqualTo(userId);
        vipUserDOExtMapper.deleteByCondition(vipUserCondition);
    }

    @Override
    public long countVipUser(long current) {
        VipUserCondition vipUserCondition = new VipUserCondition();
        VipUserCondition.Criteria criteria = vipUserCondition.createCriteria();
        criteria.andVipEndDateGreaterThan(current);
        return vipUserDOExtMapper.countByCondition(vipUserCondition);
    }

    @Override
    public List<VipUserDO> findVipUserByPage(int page, int pageSize, long current) {
        VipUserCondition vipUserCondition = new VipUserCondition();
        VipUserCondition.Criteria criteria = vipUserCondition.createCriteria();
        criteria.andVipEndDateGreaterThan(current);
        vipUserCondition.setOffset(page * pageSize);
        vipUserCondition.setLimit(pageSize);
        return vipUserDOExtMapper.selectByCondition(vipUserCondition);
    }

    @Override
    public void updateTicketLeftByIds(List<Long> vipIds) {
        vipUserDOExtMapper.updateTicketLeftByIds(vipIds);
    }
}